﻿using System;
using System.IO;
using DevExpress.Web.ASPxCallback;

using YhmOA.Model;
using YhmOA.BLL;


namespace YhmOA.Integrated.Car
{
    public partial class CarForm : FormPage<Integrated_Car_Car>
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bool isReadOnly = Mode == FormMode.ReadOnly;

                this.btnCancel.Visible = !isReadOnly;
                this.btnSave.Visible = !isReadOnly;
                base.LoadData(isReadOnly);
            }
        }

        #region Private Method
        /// <summary>
        /// 保存车辆
        /// </summary>
        /// <returns>保存是否成功</returns>
        private bool save(bool isFY)
        {
            bool isSucceed = false;
            Local = CreateInstance();

            switch (Mode)
            {
                case FormMode.ReadOnly:
                    break;
                case FormMode.Add:
                    Local.InsertDate = DateTime.Now;
                    //记录费用
                    if (isFY)
                    {
                        string sn = string.Empty;
                        Integrated_Car_CostRecordBLL.AddCostRecord(Local.PlateNum, Local.BuyPrice, CostType.Violated, string.Empty, string.Empty, out sn);
                    }
                    isSucceed = Dal.Add(Local);
                    break;
                case FormMode.Modify:
                    Local.UpdateDate = DateTime.Now;
                    isSucceed = Dal.Update(Local, this._ID);
                    break;
                default:
                    break;
            }
                
            return isSucceed;
        }

        //上传图片
        protected void uplPicPath_FileUploadComplete(object sender, DevExpress.Web.ASPxUploadControl.FileUploadCompleteEventArgs e)
        {
            string imgPath = Path.Combine("~/Integrated/UpLoadImage",
             DateTime.Now.ToFileTime() + e.UploadedFile.FileName);
            e.UploadedFile.SaveAs(Server.MapPath(imgPath));
            e.CallbackData = imgPath.Replace("~/", "");
        }

        //图片赋路径
        protected void cbPicPath_Callback(object source, CallbackEventArgs e)
        {
            ASPxCallback callbackControl = source as ASPxCallback;
            callbackControl.JSProperties["cpImgPicUrl"] = e.Parameter; 
        }

        protected void cbSave_Callback(object source, CallbackEventArgs e)
        {
            ASPxCallback callbackControl = source as ASPxCallback;
            this.txt_PicPath.ImageUrl = e.Parameter.Split('|')[0];
            bool isSucceed = this.save(bool.Parse(e.Parameter.Split('|')[1]));
            callbackControl.JSProperties["cpIsSucceed"] = isSucceed;
        }
        #endregion
    }
}